#ifndef  _adc_h_
#define _adc_h_

#include "define.h"

//#define		PHASE_DEBUG		1
//#define		VOLTAGE_DEBUG	1
//#define		CURRENT_DEBUG	1
#define		STANDARD_MODE	1


#define		Min_I_AcTH		110
#define		I_Ac_ChangedTH	80
#define		MAX_VAC			c_AD_260V		
#define		MIN_VAC			c_AD_170V				
#define		Rec_V			c_AD_200V			
#define		OverVacCntTH	5
#define		UnderVacCntTH	5
#define		AvrgTimesCntTH	2

#define		V_AVG_NUM		9	
#define		I_AVG_NUM		9		

#define		V_NUM			2		
#define		I_NUM			3		

#define		V_K_ADRL		0x00
#define		V_K_ADRH		0x01
#define		V_C_ADRL		0x02
#define		V_C_ADRH		0x03
#define		V_CAL_C_ADR		0x04

#define		I_K1_ADRL		0x05
#define		I_K1_ADRH		0x06
#define		I_C1_ADRL		0x07
#define		I_C1_ADRH		0x08
#define		I_CAL_C1_ADR	0x09

#define		I_K2_ADRL		0x0A
#define		I_K2_ADRH		0x0B
#define		I_C2_ADRL		0x0C
#define		I_C2_ADRH		0x0D
#define		I_CAL_C2_ADR	0x0E

#define		CUR0_ADRL		0x0F
#define		CUR0_ADRH		0x10
#define		CUR1_ADRL		0x11
#define		CUR1_ADRH		0x12
#define		CUR2_ADRL		0x13
#define		CUR2_ADRH		0x14

#define		CURAD0_ADRL		0x15
#define		CURAD0_ADRH		0x16
#define		CURAD1_ADRL		0x17
#define		CURAD1_ADRH		0x18
#define		CURAD2_ADRL		0x19
#define		CURAD2_ADRH		0x1A

#define		VOL0_ADRL		0x1B
#define		VOL0_ADRH		0x1C
#define		VOL1_ADRL		0x1D
#define		VOL1_ADRH		0x1E

#define		VOLAD0_ADRL		0x1F
#define		VOLAD0_ADRH		0x20
#define		VOLAD1_ADRL		0x21
#define		VOLAD1_ADRH		0x22

#define		TVOLAD0_ADRL		0x23
#define		TVOLAD0_ADRH		0x24
#define		TVOLAD1_ADRL		0x25
#define		TVOLAD1_ADRH		0x26

#define V_TOLERANCE  0 //There is a big difference between the power and no-load voltage AD value when loaded

#define c_AD_90V	(33-V_TOLERANCE)
#define c_AD_100V	(35-V_TOLERANCE)
#define c_AD_110V	(39-V_TOLERANCE)
#define c_AD_120V	(42-V_TOLERANCE)
#define c_AD_130V	(48-V_TOLERANCE)
#define c_AD_140V	(52-V_TOLERANCE)
#define c_AD_150V	(55-V_TOLERANCE)
#define c_AD_160V	(60-V_TOLERANCE)
#define c_AD_170V	(65-V_TOLERANCE)
#define c_AD_180V	(68-V_TOLERANCE)

#define c_AD_190V	(72-V_TOLERANCE)
#define c_AD_200V	(76-V_TOLERANCE)
#define c_AD_210V	(80-V_TOLERANCE)
#define c_AD_220V	(84-V_TOLERANCE)
#define c_AD_230V	(88-V_TOLERANCE)
#define c_AD_240V	(92-V_TOLERANCE)

#define c_AD_250V	(96-V_TOLERANCE)
#define c_AD_260V	(100-V_TOLERANCE)
#define c_AD_270V	(104-V_TOLERANCE)
#define c_AD_280V	(108-V_TOLERANCE)



#define		I_AD_CHANGE1	125
#define		I_AD_CHANGE2	215
#define		I_AD_CHANGE3	320
#define		I_AD_CHANGE4	440
#define		I_AD_CHANGE5	550
#define		I_AD_CHANGE6	840
#define		I_AD_CHANGE7	1100
#define		I_AD_CHANGE8	1400
#define		I_AD_CHANGE9	1700
#define		I_AD_CHANGE10	2000

#define		MAX_POWER_304	2500
#define		MAX_POWER_IRON	3500
#define		Max_Power		2000
#define		Min_Power		300

#define		T_IGBT_PowerOff_CntTH		5
#define		T_IGBT_LimitPower_CntTH		5
#define		T_IGBT_LimitPower_RecTH		T_IGBT_70	
#define		T_IGBT_SHORT	7		
#define		T_IGBT_OPEN		250		
#define		T_IGBT_30		244		
#define		T_IGBT_35		238		//35
#define		T_IGBT_40		234		//40
#define		T_IGBT_45		229		//45
#define		T_IGBT_50		224		//50
#define		T_IGBT_55		218		//55
#define		T_IGBT_60		212		//60
#define		T_IGBT_65		205		//65
#define		T_IGBT_70		197		//70
#define		T_IGBT_75		188		//75
#define		T_IGBT_80		179		//80
#define		T_IGBT_90		160		//90
#define		T_IGBT_100		140		//100
#define		T_IGBT_110		120		//110
#define		T_IGBT_120		102		//120
#define		T_IGBT_130		85		//130
#define		T_IGBT_140		71		//140
#define		T_IGBT_150		58		//150


#define		T_PAN_PRO_CntTH	5
#define		T_PAN_REC_CntTH	5
#define		T_PAN_SHORT		250		
#define		T_PAN_OPEN		3		


#define		T_PAN_25		244		//60
#define		T_PAN_30		241		//60
#define		T_PAN_40		234		//60
#define		T_PAN_50		224		//60
#define		T_PAN_60		212		//60
#define		T_PAN_70		197		//70
#define		T_PAN_80		179		//80
#define		T_PAN_90		160		//90
#define		T_PAN_100		140		//100
#define		T_PAN_110		120		//100
#define		T_PAN_120		102		//120
#define		T_PAN_130		85		//100
#define		T_PAN_140		71		//140
#define		T_PAN_150		58		//100
#define		T_PAN_160		48		//160
#define		T_PAN_170		40		//100
#define		T_PAN_180		33		//180
#define		T_PAN_190		27		//100
#define		T_PAN_200		22		//200
#define		T_PAN_220		16		//220
#define		T_PAN_240		11		//240
#define		T_PAN_250		9		//250


#define		AC_Fre					50		//HZ
#define 	Ad_Sample_Times			10		//
#define		V_Offset				0//5
#define 	I_Offset				0//80

#define		get_voltage_ADvalue		0
#define		get_current_ADvalue		1
#define		get_IGBTtemp_ADvalue	2
#define		get_TOPtemp_ADvalue		3
#define		get_PANtemp_ADvalue		4
#define		get_PANcurrent_ADvalue		5

#define		AD_CLK_SET_16MHz()		{	_sacks2 = 0; _sacks1 = 0; _sacks0 = 0;}			// FSYS/16 = 16 MHz /1 	 = 16MHz
#define		AD_CLK_SET_8MHz()		{	_sacks2 = 0; _sacks1 = 0; _sacks0 = 1;}			// FSYS/16 = 16 MHz /2 	 = 8MHz
#define		AD_CLK_SET_4MHz()		{	_sacks2 = 0; _sacks1 = 1; _sacks0 = 0;}			// FSYS/16 = 16 MHz /4 	 = 4MHz
#define		AD_CLK_SET_2MHz()		{	_sacks2 = 0; _sacks1 = 1; _sacks0 = 1;}			// FSYS/16 = 16 MHz /8 	 = 2MHz
#define		AD_CLK_SET_1MHz()		{	_sacks2 = 1; _sacks1 = 0; _sacks0 = 0;}			// FSYS/16 = 16 MHz /16 	 = 1MHz
#define		AD_CLK_SET_5KHz()		{	_sacks2 = 1; _sacks1 = 0; _sacks0 = 1;}			// FSYS/16 = 16 MHz /32 	 = 0.5MHz
#define		AD_CLK_SET_2_5KHz()		{	_sacks2 = 1; _sacks1 = 1; _sacks0 = 0;}			// FSYS/16 = 16 MHz /64 	 = 0.25MHz
#define		AD_CLK_SET_1_25KHz()	{	_sacks2 = 1; _sacks1 = 1; _sacks0 = 1;}			// FSYS/16 = 16 MHz /128 	 = 0.125MHz


#define		ADC_AUTO_Converted_Trigger_PWM0()				{ _aadcts0 = 0; _aadcts1 = 0;}
#define		ADC_AUTO_Converted_Trigger_OVP7S()				{ _aadcts0 = 1; _aadcts1 = 0;}
#define		ADC_AUTO_Converted_Trigger_Software()			{ _aadcts0 = 1; _aadcts1 = 1;}


#define		ADC_AUTO_Converted_DIS_times()                     {_autoadc2 = 0;	_autoadc1 = 0;	_autoadc0 = 0;}  //
#define 	ADC_AUTO_Converted_1_times()                       {_autoadc2 = 0;	_autoadc1 = 0;	_autoadc0 = 1;}  //
#define 	ADC_AUTO_Converted_2_times()                       {_autoadc2 = 0;	_autoadc1 = 1;	_autoadc0 = 0;}  //
#define 	ADC_AUTO_Converted_4_times()                       {_autoadc2 = 0;	_autoadc1 = 1;	_autoadc0 = 1;}  //
#define 	ADC_AUTO_Converted_8_times()                       {_autoadc2 = 1;	_autoadc1 = 0;	_autoadc0 = 0;}  //
#define 	ADC_AUTO_Converted_16_times()                      {_autoadc2 = 1;	_autoadc1 = 1;	_autoadc0 = 1;}  //

#define		ADC_Start()			    {_start=0;_start=1;_start=0;}

 /**
   * @brief ADC analog channel selection list
   */
typedef enum 
{
	ADC_CH0  = (u8)0x00, /**< Analog channel 0 */
	ADC_CH1  = (u8)0x01, /**< Analog channel 1 */
	ADC_CH2  = (u8)0x02, /**< Analog channel 2 */
	ADC_CH3  = (u8)0x03, /**< Analog channel 3 */
	ADC_CH4  = (u8)0x04, /**< Analog channel 4 */
	ADC_CH5  = (u8)0x05, /**< Analog channel 5 */
	ADC_CH6  = (u8)0x06, /**< Analog channel 6 */
	ADC_CH7  = (u8)0x07, /**< Analog channel 7 */
	ADC_CH8  = (u8)0x08, /**< Analog channel 8 */
	ADC_CH9  = (u8)0x09, /**< Analog channel 9 */
	ADC_CH10  = (u8)0x0A, /**< Analog channel 10 */
	ADC_CH11  = (u8)0x0B, /**< Analog channel 10 */
	ADC_CH12  = (u8)0x0C, /**< Analog channel 10 */
	ADC_CH_INTERNAL_Vbg		= (u8)0x20,
	ADC_CH_INTERNAL_OPAO	= (u8)0x40,
	ADC_CH_INTERNAL_OPARO	= (u8)0x60,
	ADC_CH_INTERNAL_GND		= (u8)0x80,
} ADC_Channel_TypeDef;

#define ADC_Reference_Voltage_VREF_Pin() 			{_savrs0 = 0; _savrs1 = 0;}
#define ADC_Reference_Voltage_VDD()				{_savrs0 = 0; _savrs1 = 1;}


typedef union
{
	struct
	{
		u8 Get_Power_Fg				:1;
		u8 IGBT_OTP_PowerOff_F		:1;
		u8 IGBT_OTP_LimitPower_F	:1;
		u8 Pan_MovOut				:1;
		u8 Over_Vac					:1;
		u8 Under_Vac				:1;
		u8 Rec_Vac					:1;
		u8 IGBT_Err					:1;	
	}flagBit;
	u8 m_data;
}
_ADC_FLAG;
						
typedef union
{
	struct
	{
		u8 PAN_PRO_F  		:1;
		u8 PAN_REC_F		:1;
		u8 NC				:6;
	
	}flagBit;
	u8 m_data;
}
_ADC_FLAG1;	
	
extern _ADC_FLAG	ADC_FLAG;
extern _ADC_FLAG1	ADC_FLAG1;


extern vu16		u16_PowerMea,u16_PowerMeaSum;
extern vu8		Ad_Ch_Sel,AD_Temp_IGBT, AD_TEMP_Coil, AD_Temp_PAN,AD_Temp_PAN_Quality;
extern vu16		V_Ad_Value,I_Ad_Value[2];
extern vu8		MoveInCnt,MoveOutCnt;
extern vu16 	V_Ac[V_AVG_NUM],I_Ac[I_AVG_NUM],V_AcStable,I_AcStable;
extern vu8		PowerMeaCnt;
extern vu8		PowerAddCnt;
extern vu16		V_AcH, V_AcL;
extern vu16		TempAD;

extern bit 		Ad_Samp_Flag;
extern vu16		Voltage[V_NUM],VoltageAD[V_NUM],Current[I_NUM],CurrentAD[I_NUM],TempVoltage,TempCurrent;
extern vu16		TempVoltage1, TempVoltage2;
extern vu16		TempVoltageL, TempVoltageH;	
extern vu16		TempCurrent1, TempCurrent2;
extern vu16		TempCurrentL, TempCurrentH;	

extern vu8		V_I_avg,AD_times,V_Cnt,I_Cnt,VI_Cnt;
extern vu32		Sum,SumAD;
extern vu16		TempV_AD[V_NUM],TempI_AD[I_NUM];
extern vu16		V_Calibration_K,V_Calibration_C;
extern vu16		I_Calibration_K1,I_Calibration_C1,I_Calibration_K2,I_Calibration_C2,CurrentPoint1;
extern vu8		V_Cal_C,I_Cal_C1,I_Cal_C2;

extern vu8		EEPROMdataH,EEPROMdataL;
extern vu32		TempCau;
extern vu16		Cal_VMax,Cal_Vmin,ADC_V_result,ADC_I_result;
extern vu16		I_AdSumForCal[8];
extern vu16		V_AdSumForCal[8];
extern u32		V_AdSum , I_AdSum ;
//*******************
extern vu16 	R_PowerIV,R_Iac_2048,R_Vac_64   /*,Voltage_get_line*/   , Current_get_line;

void ADC_Init(void);
void Get_Power_Temp(void);
void VoltageTemperatureDet(void);

u16 AD_Convert(u8 ADC_Channel) ;



#endif

/******************* End of file ***************/